package com.zzq.my.jetpacks.jectpackapp.ui.act

import android.util.Log
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.mcxtzhang.commonadapter.viewgroup.VGUtil
import com.mcxtzhang.commonadapter.viewgroup.adapter.cache.ViewHolder
import com.mcxtzhang.commonadapter.viewgroup.adapter.mul.IMulTypeHelper
import com.mcxtzhang.commonadapter.viewgroup.adapter.mul.MulTypeAdapter
import com.mcxtzhang.commonadapter.viewgroup.adapter.single.SingleAdapter
import com.wuyr.activitymessenger.startActivity
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.bean.K50Bean
import com.zzq.my.jetpacks.jectpackapp.bean.MulBean1
import com.zzq.my.jetpacks.jectpackapp.bean.MulBean2
import com.zzq.my.jetpacks.jectpackapp.databinding.ActivityAddviewBinding
import com.zzq.my.jetpacks.net.rxhttp.show
import com.zzq.my.jetpacks.util.loadImage


/**
 * 描述：linear addView  添加动态view 的优雅做法
 * 作者：zhuangzeqin
 * 时间: 2022/1/18-17:04
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class Linear_layout_addView_Activity : BaseActivity(com.zzq.my.jetpacks.jectpackapp.R.layout.activity_addview) {
    private val vb by viewBinding(ActivityAddviewBinding::bind)
    private var mDatas: MutableList<K50Bean>? = null
    private var llCurrent: LinearLayout? = null
    private  var llUseMore:LinearLayout? = null
    private  var llRecent:LinearLayout? = null

    private var mPosition = 0
    /**
     * 初始化数据
     */
    override fun initData() {


    }

    fun initDatas(): MutableList<K50Bean>{
        val datas: MutableList<K50Bean> = mutableListOf()
        datas.add(K50Bean("自行车1"))
        datas.add(K50Bean("自行车2"))
        datas.add(K50Bean("自行车3"))
        datas.add(K50Bean("自行车4"))
        datas.add(K50Bean("自行车5"))
        datas.add(K50Bean("自行车6"))
        datas.add(K50Bean("自行车7"))
        datas.add(K50Bean("自行车8"))
        return datas
    }

    /**
     * 初始化操作
     */
    override fun initView() {
//        llCurrent = (LinearLayout) findViewById(com.zzq.my.jetpacks.jectpackapp.R.id.llCurrent);
//        llUseMore = (LinearLayout) findViewById(com.zzq.my.jetpacks.jectpackapp.R.id.llUseMore);
//        llRecent = (LinearLayout) findViewById(com.zzq.my.jetpacks.jectpackapp.R.id.llRecent);
        mDatas =  initDatas()
        val adapter1: SingleAdapter<K50Bean> =
            object : SingleAdapter<K50Bean>(this, mDatas, com.zzq.my.jetpacks.jectpackapp.R.layout.item_50k) {
                override fun onBindViewHolder(
                    parent: ViewGroup?,
                    holder: ViewHolder,
                    data: K50Bean?,
                    pos: Int
                ) {
                    holder.setText(com.zzq.my.jetpacks.jectpackapp.R.id.tv, data?.name)
                }
            }
        //单一ItemView
//        ViewGroupUtils.addViews(llCurrent, adapter1);
//        VGUtil.Builder().setParent(vb.llCurrent).setAdapter(adapter1).build().bind()
        VGUtil.Builder().setParent(vb.llCurrent).setAdapter(adapter1).setOnItemClickListener { viewGroup, view, position ->
            Toast.makeText(this, "position：" + position + "被删除了", Toast.LENGTH_SHORT).show()
            mPosition = position
            val item  = mDatas?.get(position)
            Log.d(TAG, "initView: ${item.hashCode()}")
            startActivity<LockTableViewAct>("item" to item)
//            mDatas?.removeAt(position)
//            adapter1.notifyDatasetChanged();
        }.build().bind()
        VGUtil.Builder().setParent(vb.llRecent).setAdapter(adapter1).build().bind()
        VGUtil.Builder().setParent(vb.llUseMore).setAdapter(adapter1).build().bind()

        this.mulItem()
    }


    private fun mulItem() {


        val datas = ArrayList<IMulTypeHelper>()
        for (i in 0..9) {
            datas.add(MulBean1("http://agent-attch.oss.aliyuncs.com/%E7%8B%82%E6%AC%A2618_1653030144241_93481.jpg?OSSAccessKeyId=LTAI4FwJdDP2uQJTB3zC9t1t&Signature=w1k3%2B3zxBify4tFyq2SPajU%2BLCM%3D&Expires=4804024233"))
            datas.add(MulBean2("张旭童"))
        }


        //多种Item类型：数据结构不同 不传泛型了 使用时需要强转javaBean，判断ItemLayoutId


        //多种Item类型：数据结构不同 不传泛型了 使用时需要强转javaBean，判断ItemLayoutId
//        ViewGroupUtils.addViews(
//           vb.activityMulTypeMulBean,
//            object : MulTypeAdapter<IMulTypeHelper>(this, datas) {
//                override fun onBindViewHolder(
//                    parent: ViewGroup,
//                    holder: ViewHolder,
//                    data: IMulTypeHelper?,
//                    pos: Int
//                ) {
//                    when (data!!.itemLayoutId) {
//                        R.layout.item_image -> {
//                            val mulBean1: MulBean1 = data as MulBean1
//                            Log.d(TAG, "onBindViewHolder:mulBean1.url ${mulBean1.url}")
//                            holder.itemView.findViewById<ImageView>(R.id.iv_img).loadImage(mulBean1.url)
//                        }
//                        R.layout.item_text -> {
//                            val mulBean2: MulBean2 = data as MulBean2
//                            Log.d(TAG, "onBindViewHolder:mulBean2.name ${mulBean2.name}")
//                            holder.itemView.findViewById<TextView>(R.id.stv_item_text).text = mulBean2.name
//
//                        }
//                    }
//                }
//            })
        //V1.8.0 建议使用Builder模式构建VGUtil
        VGUtil.Builder().setParent(vb.activityMulTypeMulBean).setAdapter(object :MulTypeAdapter<IMulTypeHelper>(this,datas){
            override fun onBindViewHolder(
                parent: ViewGroup,
                holder: ViewHolder,
                data: IMulTypeHelper,
                pos: Int
            ) {
                when (data.itemLayoutId) {
                        R.layout.item_image -> {
                            val mulBean1: MulBean1 = data as MulBean1
                            Log.d(TAG, "onBindViewHolder:mulBean1.url ${mulBean1.url}")
                            holder.itemView.findViewById<ImageView>(R.id.iv_img).loadImage(mulBean1.url)
                        }
                        R.layout.item_text -> {
                            val mulBean2: MulBean2 = data as MulBean2
                            Log.d(TAG, "onBindViewHolder:mulBean2.name ${mulBean2.name}")
                            holder.itemView.findViewById<TextView>(R.id.stv_item_text).text = mulBean2.name

                        }
                    }
            }
        }).build().bind()
//        VGUtil.Builder()
//            .setParent(findViewById(R.id.activity_mul_type_mul_bean) as ViewGroup)
//            .setAdapter(object : MulTypeAdapter<Any?>(this, datas) {
//                fun onBindViewHolder(
//                    parent: ViewGroup,
//                    holder: ViewHolder,
//                    data: IMulTypeHelper?,
//                    pos: Int
//                ) {
//                    when (data!!.itemLayoutId) {
//                        R.layout.item_mulbean_1 -> {
//                            val mulBean1: MulBean1? = data as MulBean1?
//                            Glide.with(this@MulTypeMulBeanActivity)
//                                .load(mulBean1.getUrl())
//                                .into(holder.itemView as ImageView)
//                        }
//                        R.layout.item_mulbean_2 -> {
//                            val mulBean2: MulBean2? = data as MulBean2?
//                            val tv = holder.itemView as TextView
//                            tv.setText(mulBean2.getName())
//                        }
//                    }
//                }
//            })
//            .build()
//            .bind()
    }


    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {
        vb.btnChange.setOnClickListener{
            val item  = mDatas?.get(mPosition)
            item?.name?.show()
        }
    }




}